import { GameData } from "../game/GameData";
import { TaskMgr } from "../game/TaskMgr";
cc.Class({
    extends: require('BasePanel'),

    properties: {
        labCoin: cc.Label,
        labTime: cc.Label,
        toggle: cc.Toggle,
    },
    onLoad() {
        this._super();
    },

    onEnter(args, action = true) {
        this._super(args, action);
        this._updateLabel();
        G.ad.show(G.ad.AD_BANNER);
    },

    onExit(args) {
        this._super(args);
        this.toggle.isChecked = true;
        G.ad.hide(G.ad.AD_BANNER);
    },

    _updateLabel() {
        let t = this.enterArgs.time;
        if (t < 5 * 3600) {
            let h = Math.floor(t / 3600);
            let m = Math.floor((t - h * 3600) / 60);
            let s = Math.floor(t - h * 3600 - m * 60);
            this.labTime.string = `离线时长：${G.utils.prefixZero(h, 2)}:${G.utils.prefixZero(m, 2)}:${G.utils.prefixZero(s, 2)}`;
        } else {
            this.labTime.string = `离线时长：05:00:00`;
        }

        this.labCoin.string = `收益:${G.bignumber.toUnitType(this.enterArgs.coin)}金币`;
    },

    show() {
        this._super();

    },

    hide(cb) {
        this._super(cb);
    },
    onEnable() {

    },
    onDisable() {
    },

    buttonListener: function (button) {
        G.game.gameAudioMgr.playBtn();
        let name = button.node.name;
        if (name == 'btnClose') {
            this.onClose();
        } else if (name == 'btnShare') {
            G.partner.sendEvent('离线', { '类型': '离线' });
            let toggleChecked = this.toggle.isChecked;
            if (toggleChecked) {
                G.ad.show(G.ad.AD_VIDEO, (param) => {
                    G.partner.sendEvent('视频广告', { '类型': '离线' });
                    if (param.event == G.ad.EVENT_SHOW_SUCC) {
                        this.onClose();
                        GameData.addCoin(coin);
                        G.game.toast('金币+' + G.bignumber.toUnitType(coin), 1);
                        TaskMgr.addDaliyTask(5, 1);
                    } else {
                        G.partner.sendEvent('分享', { '类型': '离线' });
                        let coin = G.bignumber.mult(this.enterArgs.coin, 2)
                        G.partner.share({ title: 'Pia!(ｏ ‵-′)ノ”(ノ﹏<。)' }, () => {
                            this.onClose();
                            GameData.addCoin(coin);
                            G.game.toast('金币+' + G.bignumber.toUnitType(coin), 1);
                            TaskMgr.addDaliyTask(5, 1);
                        });
                    }
                })

            } else {
                this.onClose();
                GameData.addCoin(this.enterArgs.coin);
                G.game.toast('金币+' + G.bignumber.toUnitType(this.enterArgs.coin), 1);
            }
        }
    },

    onTouchStart() {

    },
});
